<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style type="text/css">

    .item-div{
        border: 1px solid blue;
        width: 350px;
        float: left;
        margin-left: 10px;
        margin-top: 10px;
    }

    .white-bg #search-form{
        margin-top: 10px;
        text-align:center;
        padding-top: 3px;
    }

    .item-div div{
        margin: 5px 0 5px 5px;
    }

</style>
<body class="white-bg" id="body-div" style="overflow-y:scroll;">

        <form class="form-horizontal" role="form" id="search-form">
            <div class="form-group">
                <label for="searchContent" class="col-sm-2 control-label">搜索</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="searchContent" placeholder="商品名称,店铺名称">
                </div>
                <div class="col-sm-2">
                    <button type="button" class="btn btn-info" onclick="search()">查询</button>
                </div>
            </div>
        </form>

	<div id="main-div" style="overflow-y:scroll;width: 101%"></div>	
    <!--<div class="item-div" id="item-div#actId#">
        <div>
            <label style="width: 20%;">商品标题：</label>
            <p id="goodsTitle#actId#"
                  style="float: right;width: 80%;color: red;overflow:hidden;text-overflow:ellipsis;  white-space: nowrap;">
                无</p>
        </div>
        <div>
            <label style="width: 20%;">店铺名称：</label>
            <p id="shopName#actId#"
               style="float: right;width: 80%;color: red;overflow:hidden;text-overflow:ellipsis;  white-space: nowrap;">
                无</p>
        </div>
        <div>
            <label>当前价格：</label>
            <span>￥</span><span id="curvePrice#actId#" style="color: red;">00.00</span>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <label>底价：</label>
            <span>￥</span><span id="floorPrice#actId#" style="color: red;">00.00</span>
            <button type="button" class="btn btn-primary btn-xs" style="margin-left: 20px;" onclick="refresh(#actId#)"><i class="fa fa-refresh" aria-hidden="true"></i></button>
            <button type="button" class="btn btn-primary btn-xs" onclick="updatePrice(#actId#)"><i class="fa fa-edit" aria-hidden="true"></i></button>
        </div>
        <div>
            <label>砍价信息：</label>
            <span>当前已经砍价 <span id="cutTotalTimes#actId#" style="color: red;">0</span> 次 共计砍价 ￥
                <span id="cutTotalPrice#actId#" style="color: red;">00.00</span></span>
        </div>
        <div>
            <label style="width: 20%;">砍价区间：</label>
            <p id="priceRange#actId#" title="修改价格区间" onclick="updateRange(#actId#)"
               style="float: right;width: 80%;color: red;overflow:hidden;text-overflow:ellipsis;  white-space: nowrap;text-decoration:underline;cursor:pointer ;"
               >{"max":00.00,"maxPrice":00.00,"min":00.00,"minPrice":00.00}</p>
        </div>
        <div>
            <label>砍价操作：</label>
            <span>砍</span> <input type="text" style="width: 20%" placeholder="次数" id="cutTimes#actId#"  onkeyup="value=value.replace(/[^\d]/g,'')"/> <span>次</span>
            <button type="button" class="btn btn-primary btn-xs" style="margin-left: 20px;" onclick="cutPrice(#actId#)">砍价</button>
            &nbsp;&nbsp;&nbsp;&nbsp;<span id="cutedTimes#actId#" style="display: none;">已砍价</span>
        </div>
    </div>-->

    <div th:include="include::footer"></div>
    <script type="text/javascript">
        var prefix = ctx + "goodsAct/goodsAct";
        var html = "<div class=\"item-div\" id=\"item-div#actId#\">\n" +
            "        <div>\n" +
            "            <label style=\"width: 20%;\">商品标题：</label>\n" +
            "            <p id=\"goodsTitle#actId#\"\n" +
            "                  style=\"float: right;width: 80%;color: red;overflow:hidden;text-overflow:ellipsis;  white-space: nowrap;\">\n" +
            "                无</p>\n" +
            "        </div>\n" +
            "        <div>\n" +
            "            <label style=\"width: 20%;\">店铺名称：</label>\n" +
            "            <p id=\"shopName#actId#\"\n" +
            "               style=\"float: right;width: 80%;color: red;overflow:hidden;text-overflow:ellipsis;  white-space: nowrap;\">\n" +
            "                无</p>\n" +
            "        </div>\n" +
            "        <div>\n" +
            "            <label>当前价格：</label>\n" +
            "            <span>￥</span><span id=\"curvePrice#actId#\" style=\"color: red;\">00.00</span>\n" +
            "            &nbsp;&nbsp;&nbsp;&nbsp;\n" +
            "            <label>底价：</label>\n" +
            "            <span>￥</span><span id=\"floorPrice#actId#\" style=\"color: red;\">00.00</span>\n" +
            "            <button type=\"button\" class=\"btn btn-primary btn-xs\" style=\"margin-left: 20px;\" onclick=\"refresh(#actId#)\"><i class=\"fa fa-refresh\" aria-hidden=\"true\"></i></button>\n" +
            "            <button type=\"button\" class=\"btn btn-primary btn-xs\" onclick=\"updatePrice(#actId#)\"><i class=\"fa fa-edit\" aria-hidden=\"true\"></i></button>\n" +
            "        </div>\n" +
            "        <div>\n" +
            "            <label>砍价信息：</label>\n" +
            "            <span>当前已经砍价 <span id=\"cutTotalTimes#actId#\" style=\"color: red;\">0</span> 次 共计砍价 ￥\n" +
            "                <span id=\"cutTotalPrice#actId#\" style=\"color: red;\">00.00</span></span>\n" +
            "        </div>\n" +
            "        <div>\n" +
            "            <label style=\"width: 20%;\">砍价区间：</label>\n" +
            "            <p id=\"priceRange#actId#\" title=\"修改价格区间\" onclick=\"updateRange(#actId#)\"\n" +
            "               style=\"float: right;width: 80%;color: red;overflow:hidden;text-overflow:ellipsis;  white-space: nowrap;text-decoration:underline;cursor:pointer ;\"\n" +
            "               >{\"max\":00.00,\"maxPrice\":00.00,\"min\":00.00,\"minPrice\":00.00}</p>\n" +
            "        </div>\n" +
            "        <div>\n" +
            "            <label>砍价操作：</label>\n" +
            "            <span>砍</span> <input type=\"text\" style=\"width: 20%\" placeholder=\"次数\" id=\"cutTimes#actId#\"  onkeyup=\"value=value.replace(/[^\\d]/g,'')\"/> <span>次</span>\n" +
            "            <button type=\"button\" class=\"btn btn-primary btn-xs\" style=\"margin-left: 20px;\" onclick=\"cutPrice(#actId#)\">砍价</button>\n" +
            "            &nbsp;&nbsp;&nbsp;&nbsp;<span id=\"cutedTimes#actId#\" style=\"display: none;\">已砍价</span>\n" +
            "        </div>\n" +
            "    </div>";

        $(function () {
            init();
        });

        function init() {
            var res = getData('goods/cutPriceList', {});
            if (res.code == 200) {
                var datas = res.data;
                for (var i = 0; i < datas.length; i++) {
                    var obj = datas[i];
                    var actId = obj.act_id;
                    var htmlplus = html;
                    htmlplus = htmlplus.replace(/#actId#/g, actId);
                    $('#main-div').append(htmlplus);
                    $('#goodsTitle' + actId).html(obj.goods_title);
                    $('#shopName' + actId).html(obj.shop_name);
                    $('#curvePrice' + actId).html(obj.curve_price);
                    $('#floorPrice' + actId).html(obj.floor_price);
                    $('#cutTotalTimes' + actId).html(obj.cut_count);
                    $('#cutTotalPrice' + actId).html(obj.sum_cut_price);
                    $('#priceRange' + actId).html(obj.price_range);
                    $('#priceRange' + actId).attr('title',obj.price_range);
                }
            } else {
                $.modal.alert('请求失败', 'warning');
            }
        }

        function cutPrice(actId){
            var cutTimes = $('#cutTimes'+actId).val();
            if (isEmpty(cutTimes)) {
                $.modal.alert('请输入砍价次数', 'warning');
                return;
            }
            if (cutTimes > 300) {
                $.modal.alert('超出最大次数！最大值300次！', 'warning');
                return;
            }
            $.modal.confirm("确定要砍价" + cutTimes + "次?", function () {
                $.ajax({
                    url : prefix + "/gbargain",
                    type : 'POST',
                    data : {
                        "actIds" : actId,
                        "count" : cutTimes
                    },
                    dataType : "json",
                    success: function (data) {
                        if (data.code == 200) {
                            $.modal.alert('砍价操作成功,后台正在努力砍价中...', 'success');
                            //$('#cutedTimes' + actId).show();
                            //$('#cutedTimes' + actId).html(cutTimes + ' 次');
                            $('#item-div' + actId).css("background-color", "#d9cdff");
                            $('#cutTimes'+actId).val('');
                            //layer.close(index);
                        } else {
                            $.modal.alert(data.message, 'warning');
                        }
                    }
                });
            });
        }

        function refresh(actId) {
            $.modal.loading('数据加载中...');
            var res = getData('goodsAct/goodsAct/cutPriceList', {
                "actId": actId
            });
            if (res.code == 200) {
                var goodsAct = res.data[0];
                //$('#goodsTitle' + actId).html(goodsAct.goods_title);
                //$('#shopName' + actId).html(goodsAct.shop_name);
                $('#curvePrice' + actId).html(goodsAct.curve_price);
                $('#floorPrice' + actId).html(goodsAct.floor_price);
                $('#cutTotalTimes' + actId).html(goodsAct.cut_count);
                $('#cutTotalPrice' + actId).html(goodsAct.sum_cut_price);
                $('#priceRange' + actId).html(goodsAct.price_range);
                $('#priceRange' + actId).attr('title', goodsAct.price_range);
                $.modal.closeLoading();
            } else {
                $.modal.alert('请求失败', 'error');
                $.modal.closeLoading();
            }
        }

        function search() {
            var searchContent = $('#searchContent').val();
            var res = getData('goodsAct/goodsAct/cutPriceList', {
                "searchContent": searchContent
            });
            if (res.code == 200) {
                $('.item-div').remove();
                var datas = res.data;
                for (var i = 0; i < datas.length; i++) {
                    var obj = datas[i];
                    var actId = obj.act_id;
                    var htmlplus = html;
                    htmlplus = htmlplus.replace(/#actId#/g, actId);
                    $('#main-div').append(htmlplus);
                    $('#goodsTitle' + actId).html(obj.goods_title);
                    $('#shopName' + actId).html(obj.shop_name);
                    $('#curvePrice' + actId).html(obj.curve_price);
                    $('#floorPrice' + actId).html(obj.floor_price);
                    $('#cutTotalTimes' + actId).html(obj.cut_count);
                    $('#cutTotalPrice' + actId).html(obj.sum_cut_price);
                    $('#priceRange' + actId).html(obj.price_range);
                    $('#priceRange' + actId).attr('title',obj.price_range);
                }
            } else {
                $.modal.alert('请求失败', 'warning');
            }
            //$("#search-form").reset();
        }

        function updatePrice(_actId) {
            layer.open({
                type: 2,
                title: '修改价格【敏感操作,谨慎操作】',
                area: ['50%', '50%'], //自定义文本域宽高
                content: prefix + '/update/price?actId='+_actId,
                btn: ['确定', '取消'],
                success: function (layero, index) {

                },
                //确定按钮回调
                yes: function (index, layero) {
                    var callbackData = window["layui-layer-iframe" + index].submitHandler();
                    if (!callbackData) {
                        console.log('validate not pass...');
                    } else {
                        if ('unEdit' == callbackData) {
                            layer.close(index);
                        } else {
                            $.modal.loading('数据加载中...');
                            //关闭窗口
                            layer.close(index);
                            console.log(callbackData);
                            var res = getData('goodsAct/goodsAct/update/price', callbackData);
                            if (res.code == 200) {
                                $.modal.closeLoading();
                                refresh(_actId);
                            } else {
                                $.modal.closeLoading();
                                $.modal.alert('请求失败', 'error');
                            }
                        }
                    }
                },
                //结束
                end: function () {

                }
            });

        }

        function updateRange(_actId) {
            layer.open({
                type: 2,
                title: '修改砍价限制【敏感操作,谨慎操作】',
                area: ['50%', '50%'], //自定义文本域宽高
                content: prefix + '/update/range?actId='+_actId,
                btn: ['确定', '取消'],
                success: function (layero, index) {

                },
                //确定按钮回调
                yes: function (index, layero) {
                    var callbackData = window["layui-layer-iframe" + index].submitHandler();
                    if (!callbackData) {
                        console.log('validate not pass...');
                    } else {
                        if ('unEdit' == callbackData) {
                            layer.close(index);
                        } else {
                            $.modal.loading('数据加载中...');
                            //关闭窗口
                            layer.close(index);
                            console.log(callbackData);
                            var res = getData('goodsAct/goodsAct/update/range', callbackData);
                            if (res.code == 200) {
                                $.modal.closeLoading();
                                refresh(_actId);
                            } else {
                                $.modal.closeLoading();
                                $.modal.alert('请求失败', 'error');
                            }
                        }
                    }
                },
                //结束
                end: function () {

                }
            });
        }

    </script>
</body>
</html>